$(function() {
//	$("#top_menu").load("../../../../uiframe/Top.html", function(){
//		$("#xitongshezhi").addClass("active");
//	}); 
	
	// 加载数据列表
	getList();
	
//	//监听窗口大小变化
//	window.onresize = function(){
//		setTimeout(domresize,300);
//	};
//	//改变表格宽高
//	function domresize(){
//		$('#tt').datagrid('resize',{  
//			height:$("#body").height()-$('#search_area').height(),
//			width:$("#body").width()
//		});
//	}
	
	//初始化dategrid   可定制高宽，标题
	$('#tt').datagrid({   
	    url:null,   
	    pagination:true,   
	    pageSize:_pageSize,   
	    pageNumber:1,   
	    rownumbers:true,
	    singleSelect:true,
	    title:'业务管理 > 药品字典库管理 ',
    	width:$("#body").width(),
    	height:_height,
    	toolbar:"#toolbar"

	});   
	
	//翻页设置
	$('#tt').datagrid('getPager').pagination({   
	    displayMsg:'当前显示从 [{from}] 到 [{to}] 共[{total}]条记录',   
	    onSelectPage : function(pPageIndex, pPageSize) {   
	        //改变opts.pageNumber和opts.pageSize的参数值，用于下次查询传给数据层查询指定页码的数据   
	        var gridOpts = $('#tt').datagrid('options');   
	        gridOpts.pageNumber = pPageIndex;   
	        gridOpts.pageSize = pPageSize;     
	        
	        //异步获取数据到javascript对象，入参为查询条件和页码信息   
	         
	        //使用loadDate方法加载Dao层返回的数据   
	        $('#tt').datagrid('loadData',{"total" : bannerArray.length,"rows" : getPageList(bannerArray,pPageIndex,pPageSize)});   
	    }   
	});
	
	
	//定义表单验证方法
	$('#fm').form({
		onSubmit : function() {
			// 进行表单验证
		
			// 如果返回false阻止提交
			return $(this).form('validate');
		},
		success : function(data) {
			
			saveuser();
			
			
		}
	});
	
	//$("#fm").validate();
	
	
});


//缓存用户列表
var bannerArray;
var curpage=1;
//获取列表数据
function getList(){
	var urlStr = serverPath + "/server/doctor/diabetes/knowledgeBase.do";
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"'+getCookie('sessionId')+'",'+
	    '	   "uid":"'+getCookie('Uid')+'",'+
	    '	   "method":"getAllList",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	                '{}'+
	    '	   ]'+
	    '	}';
	UseTKAjax(urlStr, jsonStr, document.cookie, task, '1');
}



//处理用户列表
function task(nodeArray) {
	if (nodeArray && nodeArray.resultlist.length > 0) {
		//排序
		bannerArray=nodeArray.resultlist[0].resultlist;
		$('#tt').datagrid('loadData',{
			"total": bannerArray.length,
			"rows":getPageList(bannerArray,1,_pageSize)
		});
	} 
}

//显示新增窗口
function newuser() {
    $("#dlg").dialog("open").dialog('setTitle', '新增');
    $('#hidtype').val("newdata");
}

//提交新增结果
function submform() {
	
	$('#fm').submit();
}

//显示编辑窗口
function edituser() {
    var row = $("#tt").datagrid("getSelected");
    
    if (row) {
        $("#dlg").dialog("open").dialog('setTitle', '编辑');
      //  $("#fm").form("clear");
        $("#fm").form("load", row);
       
    	$('#hidtype').val("editdata");
    	
    }
}

//保存表单数据
function saveuser(){
	
	var hidtype = $('#hidtype').val();
	
	var urlStr = serverPath + "/server/doctor/diabetes/knowledgeBase.do";

	if(hidtype=='editdata'){
		var jsonStr = 
		    '{'+
		    '	   "requestid":"",'+
		    '	   "requestsource":"diabetes",'+
		    '	   "sessionid":"'+getCookie('sessionId')+'",'+
		    '	   "uid":"'+getCookie('Uid')+'",'+
		    '	   "method":"updateKnowledge",'+
		    '	   "version":"1.0",'+
		    '	   "requestlist":'+
		    '	   ['+
		                getInputData('fm')+
		    '	   ]'+
		    '	}';
		UseTKAjax(urlStr, jsonStr, document.cookie, reloadData, '1');
	}else{
		var jsonStr = 
		    '{'+
		    '	   "requestid":"",'+
		    '	   "requestsource":"diabetes",'+
		    '	   "sessionid":"'+getCookie('sessionId')+'",'+
		    '	   "uid":"'+getCookie('Uid')+'",'+
		    '	   "method":"addKnowledge",'+
		    '	   "version":"1.0",'+
		    '	   "requestlist":'+
		    '	   ['+
		                getInputData('fm')+
		    '	   ]'+
		    '	}';
		UseTKAjax(urlStr, jsonStr, document.cookie, reloadData, '1');
	}
	$("#fm").form("clear");
	$("#dlg").dialog("close");
}


//删除用户
function delUser() {
    var row = $('#tt').datagrid('getSelected');
    if (row) {
        $.messager.confirm('确认', '确定要删除记录吗?', function (r) {
            if (r) {
            	//调用删除的ajax
            	var urlStr = serverPath + "/server/doctor/diabetes/knowledgeBase.do";
        		var jsonStr = 
        		    '{'+
        		    '	   "requestid":"",'+
        		    '	   "requestsource":"diabetes",'+
        		    '	   "sessionid":"'+getCookie('sessionId')+'",'+
        		    '	   "uid":"'+getCookie('Uid')+'",'+
        		    '	   "method":"delKnowledge",'+
        		    '	   "version":"1.0",'+
        		    '	   "requestlist":'+
        		    '	   [{'+
        		               "knowledge_id :" + row.knowledge_id +
        		    '	   }]'+
        		    '	}';
               UseTKAjax(urlStr, jsonStr, document.cookie, reloadData, '1');	
            }
        });
    }
}


function query(){
	var condition = $("#btn-query").val();
	var urlStr = serverPath + "/server/doctor/diabetes/knowledgeBase.do";
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"'+getCookie('sessionId')+'",'+
	    '	   "uid":"'+getCookie('Uid')+'",'+
	    '	   "method":"queryKnowledge",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   [{'+
	    '           "condition":"' + condition + '"'+
	    '	  }]'+
	    '	}';
   UseTKAjax(urlStr, jsonStr, document.cookie, task, '1');
}

function cancle(){
	$("#fm").form("clear");
	$('#dlg').dialog('close');
}

function reloadData(narrary){
	console.log(narrary);
	if(narrary.resultcode == '0'){
		alert("操作成功！");
	}else{
		alert('操作失败！');
	}
	getList();
}
